package com.chy.order.service.impl;

import com.chy.commonutils.ordervo.CourseWebVoOrder;
import com.chy.commonutils.ordervo.UcenterMemberOrder;
import com.chy.order.client.EduClient;
import com.chy.order.client.UcenterClient;
import com.chy.order.pojo.TOrder;
import com.chy.order.mapper.OrderMapper;
import com.chy.order.service.OrderService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.chy.order.utils.OrderNoUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 订单 服务实现类
 * </p>
 *
 * @author chy
 * @since 2020-11-02
 */
@Service
public class OrderServiceImpl extends ServiceImpl<OrderMapper, TOrder> implements OrderService {

    @Autowired
    private EduClient eduClient;
    @Autowired
    private UcenterClient ucenterClient;

    // 根据课程id，用户id生成订单
    @Override
    public String createOrders(String courseId, String memberId) {
        // 通过远程调用根据用户id获取用户信息
        UcenterMemberOrder userInfoOrder = ucenterClient.getUserInfoOrder(memberId);

        // 通过远程调用根据课程id获取课程信息
        CourseWebVoOrder courseInfoOrder = eduClient.getCourseInfoOrder(courseId);

        // 创建Order对象，向order对象里面设置需要数据
        TOrder tOrder = new TOrder();
        tOrder.setOrderNo(OrderNoUtil.getOrderNo()); // 订单号
        tOrder.setCourseId(courseId); // 课程id
        tOrder.setCourseTitle(courseInfoOrder.getTitle());
        tOrder.setCourseCover(courseInfoOrder.getCover());
        tOrder.setTeacherName(courseInfoOrder.getTeacherName());
        tOrder.setTotalFee(courseInfoOrder.getPrice());
        tOrder.setMemberId(memberId);
        tOrder.setMobile(userInfoOrder.getMobile());
        tOrder.setNickname(userInfoOrder.getNickname());
        tOrder.setStatus(0); // 支付状态 （0：未支付 1：已支付）
        tOrder.setPayType(1); // 支付类型，微信 1

        baseMapper.insert(tOrder);
        // 返回订单号
        return tOrder.getOrderNo();
    }
}
